const express = require('express');
const path = require('path');
const { api } = require('./config/app');
const { notFoundHandler, errorHandler } = require('./middleware/errorHandler');
const { registerRoutes } = require('./routes');

const app = express();

// 中间件
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));

// 静态文件服务
app.use(express.static(path.join(__dirname, '../public')));

// API 路由 - 使用自动注册
registerRoutes(app, api.prefix);

// SPA路由处理
const spaRoutes = require('./routes/spa');
app.use('/', spaRoutes);

// 404 错误处理
app.use(notFoundHandler);

// 错误处理中间件
app.use(errorHandler);

module.exports = app;